#pragma once

#ifdef _WIN32
#pragma warning( disable: 4251 )
#endif

#include <throwable.h>
#include <sstream>
#include <QStringList>
#include <QString>

#define THROW_EXP( error)  throw Exception(__FILE__, __LINE__,  (error))

namespace apollo
{
	namespace core
	{

    /*
     * Base class for all exceptions.
     */
    class Exception : public Throwable {
	public:
		enum ErrorLevel {INFO, WARNING, ERROR};
    protected:

		QString messageKey;

		QStringList parameters;

		ErrorLevel errorLevel;

        /**
         * The cause of this exception.
         */
        QString message;

        /**
         * The Exception that caused this one to be thrown.
         */
        std::exception* cause;

        /**
         * The stack trace.
         */
        std::vector< std::pair< QString, int> > stackTrace;

    public:

        /**
         * Default Constructor
         */
        Exception() throw();

        /**
         * Copy Constructor
         *
         * @param ex
         *      The <code>Exception</code> instance to copy.
         */
        Exception( const Exception& ex ) throw();

		Exception::Exception( const char* file, const int lineNumber,
			const QString &message ,const std::exception* cause = NULL) throw();

		Exception::Exception( const char* file, const int lineNumber, 
			const QString &message,
			const QString &messageKey, const QStringList& parameters,  ErrorLevel errorLevel = ERROR,
			const std::exception* cause = NULL) throw();


        /**
         * Constructor
         *
         * @param cause
         *      Pointer to the exception that caused this one to
         *      be thrown, the object is cloned caller retains ownership.
         */
        Exception( const std::exception* cause ) throw();


        virtual ~Exception() throw();

        /**
         * Gets the message for this exception.
         * @return Text formatted error message
         */
        virtual QString getMessage() const {
            return message;
        }

		virtual QString getLocaleMessage() const;

		 QString getMessageKey() const
		{
			return messageKey;
		}

		 QStringList getMessageParameters() const
		{
			return parameters;
		}

		ErrorLevel getErrorLevel() const
		{
			return errorLevel;
		}
        /**
         * Gets the exception that caused this one to be thrown, this allows
         * for chaining of exceptions in the case of a method that throws only
         * a particular exception but wishes to allow for the real causal
         * exception to be passed only in case the caller knows about that
         * type of exception and wishes to respond to it.
         * @returns a const pointer reference to the causal exception, if there
         * was no cause associated with this exception then NULL is returned.
         */
        virtual const std::exception* getCause() const {
            return this->cause;
        }

        /**
         * Initializes the contained cause exception with the one given.  A copy
         * is made to avoid ownership issues.
         * @param cause The exception that was the cause of this one.
         */
        virtual void initCause( const std::exception* cause );

        /**
         * Implement method from std::exception
         * @return the const char* of <code>getMessage()</code>.
         */
        virtual const char* what() const throw (){
            return message.toStdString().c_str();
        }

		
		virtual void setMessage( QString &message );

        /**
         * Adds a file/line number to the stack trace.
         * @param file The name of the file calling this method (use __FILE__).
         * @param lineNumber The line number in the calling file (use __LINE__).
         */
        virtual void setMark( const char* file, const int lineNumber );

        /**
         * Clones this exception.  This is useful for cases where you need
         * to preserve the type of the original exception as well as the message.
         * All subclasses should override.
         * @return Copy of this Exception object
         */
        virtual Exception* clone() const;

        /**
         * Provides the stack trace for every point where
         * this exception was caught, marked, and rethrown.  The first
         * item in the returned vector is the first point where the mark
         * was set (e.g. where the exception was created).
         * @return the stack trace.
         */
        virtual std::vector< std::pair< QString, int> > getStackTrace() const;

        /**
         * Prints the stack trace to std::err
         */
        virtual void printStackTrace() const;

        /**
         * Prints the stack trace to the given output stream.
         * @param stream the target output stream.
         */
        virtual void printStackTrace( std::ostream& stream ) const;

        /**
         * Gets the stack trace as one contiguous string.
         * @return string with formatted stack trace data
         */
        virtual QString getStackTraceString() const;

        /**
         * Assignment operator.
         * @param ex const reference to another Exception
         */
        Exception& operator =( const Exception& ex );

    protected:

        virtual void setStackTrace(
            const std::vector< std::pair< QString, int> >& trace );

        //virtual void buildMessage( const char* format, va_list& vargs );

   };

}}

